﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using UnityEditorInternal;
    using UnityEngine;

    internal class InspectorWindow : EditorWindow, ICanHazCustomMenu
    {
        [CompilerGenerated]
        private static Func<UnityEngine.Object, GameObject> <>f__am$cacheB;
        private const float kBottomToolbarHeight = 17f;
        private static readonly List<InspectorWindow> m_AllInspectors = new List<InspectorWindow>();
        public InspectorMode m_InspectorMode;
        private Editor m_LastInteractedEditor;
        [SerializeField]
        private PreviewResizer m_PreviewResizer = new PreviewResizer();
        [SerializeField]
        private PreviewWindow m_PreviewWindow;
        private bool m_ResetKeyboardControl;
        public Vector2 m_ScrollPosition;
        private Styles m_Styles;
        protected ActiveEditorTracker m_Tracker;
        private TypeSelectionList m_TypeSelectionList;
        public static InspectorWindow s_CurrentInspectorWindow;

        private void AddComponentButton(Editor[] editors)
        {
            Editor firstNonImportInspectorEditor = this.GetFirstNonImportInspectorEditor(editors);
            if (((firstNonImportInspectorEditor != null) && (firstNonImportInspectorEditor.target != null)) && ((firstNonImportInspectorEditor.target is GameObject) && firstNonImportInspectorEditor.IsEnabled()))
            {
                EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
                GUIContent content = new GUIContent("Add Component");
                Rect position = GUILayoutUtility.GetRect(content, this.styles.addComponentButtonStyle, null);
                position.y += 10f;
                position.x += (position.width - 230f) / 2f;
                position.width = 230f;
                if (Event.current.type == EventType.Repaint)
                {
                    this.DrawSplitLine(position.y - 11f);
                }
                Event current = Event.current;
                bool flag = false;
                if ((current.type == EventType.ExecuteCommand) && (current.commandName == "OpenAddComponentDropdown"))
                {
                    flag = true;
                    current.Use();
                }
                if (EditorGUI.ButtonMouseDown(position, content, FocusType.Passive, this.styles.addComponentButtonStyle) || flag)
                {
                    if (<>f__am$cacheB == null)
                    {
                        <>f__am$cacheB = o => (GameObject) o;
                    }
                    if (AddComponentWindow.Show(position, Enumerable.Select<UnityEngine.Object, GameObject>(firstNonImportInspectorEditor.targets, <>f__am$cacheB).ToArray<GameObject>()))
                    {
                        GUIUtility.ExitGUI();
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }

        public virtual void AddItemsToMenu(GenericMenu menu)
        {
            menu.AddItem(new GUIContent("Normal"), this.m_InspectorMode == InspectorMode.Normal, new GenericMenu.MenuFunction(this.SetNormal));
            menu.AddItem(new GUIContent("Debug"), this.m_InspectorMode == InspectorMode.Debug, new GenericMenu.MenuFunction(this.SetDebug));
            if (Unsupported.IsDeveloperBuild())
            {
                menu.AddItem(new GUIContent("Debug-Internal"), this.m_InspectorMode == InspectorMode.DebugInternal, new GenericMenu.MenuFunction(this.SetDebugInternal));
            }
            menu.AddSeparator(string.Empty);
            menu.AddItem(new GUIContent("Lock"), (this.m_Tracker != null) && this.isLocked, new GenericMenu.MenuFunction(this.FlipLocked));
        }

        private void Awake()
        {
            if (!m_AllInspectors.Contains(this))
            {
                m_AllInspectors.Add(this);
            }
        }

        private bool CanPreview(UnityEngine.Object obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (obj is GameObject)
            {
                return EditorUtility.IsPersistent(obj);
            }
            return true;
        }

        private void CheckDragAndDrop(Editor[] editors)
        {
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandHeight(true) };
            Rect rect = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, options);
            if (rect.Contains(Event.current.mousePosition))
            {
                Editor firstNonImportInspectorEditor = this.GetFirstNonImportInspectorEditor(editors);
                if (firstNonImportInspectorEditor != null)
                {
                    DoInspectorDragAndDrop(rect, firstNonImportInspectorEditor.targets);
                }
                if (Event.current.type == EventType.MouseDown)
                {
                    GUIUtility.keyboardControl = 0;
                    Event.current.Use();
                }
            }
        }

        protected virtual void CreateTracker()
        {
            <CreateTracker>c__AnonStorey3B storeyb = new <CreateTracker>c__AnonStorey3B();
            if (this.m_Tracker == null)
            {
                storeyb.sharedTracker = ActiveEditorTracker.sharedTracker;
                bool flag = Enumerable.Any<InspectorWindow>(m_AllInspectors, new Func<InspectorWindow, bool>(storeyb.<>m__6E));
                this.m_Tracker = !flag ? ActiveEditorTracker.sharedTracker : new ActiveEditorTracker();
                this.m_Tracker.inspectorMode = this.m_InspectorMode;
                this.m_Tracker.RebuildIfNecessary();
            }
        }

        private void DetachPreview()
        {
            Event.current.Use();
            this.m_PreviewWindow = ScriptableObject.CreateInstance(typeof(PreviewWindow)) as PreviewWindow;
            this.m_PreviewWindow.SetParentInspector(this);
            this.m_PreviewWindow.Show();
            base.Repaint();
            GUIUtility.ExitGUI();
        }

        private static void DoInspectorDragAndDrop(Rect rect, UnityEngine.Object[] targets)
        {
            if (Dragging(rect))
            {
                DragAndDrop.visualMode = InternalEditorUtility.InspectorWindowDrag(targets, Event.current.type == EventType.DragPerform);
                if (Event.current.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();
                }
            }
        }

        private static bool Dragging(Rect rect)
        {
            return (((Event.current.type == EventType.DragUpdated) || (Event.current.type == EventType.DragPerform)) && rect.Contains(Event.current.mousePosition));
        }

        private void DrawEditors(bool isRepaintEvent, Editor[] editors, bool eyeDropperDirty)
        {
            Editor.m_AllowMultiObjectAccess = true;
            bool flag = false;
            Rect position = new Rect();
            bool flag2 = true;
            for (int i = 0; i < editors.Length; i++)
            {
                Editor editor = editors[i];
                if (!editor.hideInspector)
                {
                    bool isInspectorExpanded;
                    UnityEngine.Object target = editor.target;
                    GUIUtility.GetControlID(target.GetInstanceID(), FocusType.Passive);
                    EditorGUIUtility.ResetGUIState();
                    GUILayoutGroup topLevel = GUILayoutUtility.current.topLevel;
                    int visible = this.m_Tracker.GetVisible(i);
                    if (visible == -1)
                    {
                        isInspectorExpanded = InternalEditorUtility.GetIsInspectorExpanded(target);
                        this.m_Tracker.SetVisible(i, !isInspectorExpanded ? 0 : 1);
                    }
                    else
                    {
                        isInspectorExpanded = visible == 1;
                    }
                    bool isDirty = editor.isInspectorDirty || eyeDropperDirty;
                    if (isRepaintEvent)
                    {
                        editor.isInspectorDirty = false;
                    }
                    if (!this.ShouldCullEditor(editors, i))
                    {
                        if ((editor is AssetImporterInspector) && (editors.Length > 1))
                        {
                            (editor as AssetImporterInspector).m_AssetEditor = editors[1];
                        }
                        bool flag5 = ((AssetDatabase.IsMainAsset(target) || AssetDatabase.IsSubAsset(target)) || (i == 0)) || (target is Material);
                        if (flag2)
                        {
                            DockArea parent = base.m_Parent as DockArea;
                            if (parent != null)
                            {
                                parent.tabStyle = "dragtabbright";
                            }
                            GUILayout.Space(0f);
                            flag2 = false;
                        }
                        if (flag5)
                        {
                            string message = string.Empty;
                            bool flag6 = editor.IsOpenForEdit(out message);
                            if (flag)
                            {
                                flag = false;
                                GUILayout.Space(15f);
                                position = GUILayoutUtility.GetRect((float) 16f, (float) 16f);
                                position.height = 17f;
                            }
                            isInspectorExpanded = true;
                            EditorGUI.BeginDisabledGroup(!flag6);
                            editor.OnHeaderGUI();
                            EditorGUI.EndDisabledGroup();
                        }
                        if (editor.target is AssetImporter)
                        {
                            flag = true;
                        }
                        bool flag7 = false;
                        if (((editor is GenericInspector) && (CustomEditorAttributes.FindCustomEditorType(target, false) != null)) && (this.m_InspectorMode != InspectorMode.DebugInternal))
                        {
                            if (this.m_InspectorMode == InspectorMode.Normal)
                            {
                                flag7 = true;
                            }
                            else if (target is AssetImporter)
                            {
                                flag7 = true;
                            }
                        }
                        if (!flag5)
                        {
                            EditorGUI.BeginDisabledGroup(!editor.IsEnabled());
                            bool isExpanded = EditorGUILayout.InspectorTitlebar(isInspectorExpanded, editor.targets);
                            if (isInspectorExpanded != isExpanded)
                            {
                                this.m_Tracker.SetVisible(i, !isExpanded ? 0 : 1);
                                InternalEditorUtility.SetIsInspectorExpanded(target, isExpanded);
                                if (isExpanded)
                                {
                                    this.m_LastInteractedEditor = editor;
                                }
                                else if (this.m_LastInteractedEditor == editor)
                                {
                                    this.m_LastInteractedEditor = null;
                                }
                            }
                            EditorGUI.EndDisabledGroup();
                        }
                        if (flag7 && isInspectorExpanded)
                        {
                            GUILayout.Label("Multi-object editing not supported.", EditorStyles.helpBox, new GUILayoutOption[0]);
                        }
                        else
                        {
                            float num3;
                            OptimizedGUIBlock block;
                            EditorGUI.BeginDisabledGroup(!editor.IsEnabled());
                            GenericInspector inspector = editor as GenericInspector;
                            if (inspector != null)
                            {
                                inspector.m_InspectorMode = this.m_InspectorMode;
                            }
                            if (inspector != null)
                            {
                                EditorGUIUtility.LookLikeInspector();
                            }
                            else
                            {
                                EditorGUIUtility.LookLikeControls();
                            }
                            if (editor.GetOptimizedGUIBlock(isDirty, isInspectorExpanded, out block, out num3))
                            {
                                Rect rect = GUILayoutUtility.GetRect(0f, !isInspectorExpanded ? 0f : num3);
                                this.HandleLastInteractedEditor(rect, editor);
                                if (Event.current.type == EventType.Layout)
                                {
                                    continue;
                                }
                                if (block.Begin(isDirty, rect) && isInspectorExpanded)
                                {
                                    GUI.changed = false;
                                    editor.OnOptimizedInspectorGUI(rect);
                                }
                                block.End();
                            }
                            else
                            {
                                if (isInspectorExpanded)
                                {
                                    Rect componentRect = EditorGUILayout.BeginVertical(new GUILayoutOption[0]);
                                    this.HandleLastInteractedEditor(componentRect, editor);
                                    if (inspector != null)
                                    {
                                        EditorGUIUtility.LookLikeInspector();
                                    }
                                    else
                                    {
                                        EditorGUIUtility.LookLikeControls();
                                    }
                                    GUI.changed = false;
                                    try
                                    {
                                        editor.OnInspectorGUI();
                                    }
                                    catch (System.Exception exception)
                                    {
                                        if (exception is ExitGUIException)
                                        {
                                            throw;
                                        }
                                        Debug.LogException(exception);
                                    }
                                    EditorGUILayout.EndVertical();
                                }
                                if (Event.current.type == EventType.Used)
                                {
                                    return;
                                }
                            }
                            EditorGUI.EndDisabledGroup();
                            if (GUILayoutUtility.current.topLevel != topLevel)
                            {
                                if (!GUILayoutUtility.current.layoutGroups.Contains(topLevel))
                                {
                                    Debug.LogError("Expected top level layout group missing! Too many GUILayout.EndScrollView/EndVertical/EndHorizontal?");
                                    GUIUtility.ExitGUI();
                                }
                                else
                                {
                                    Debug.LogWarning("Unexpected top level layout group! Missing GUILayout.EndScrollView/EndVertical/EndHorizontal?");
                                    while (GUILayoutUtility.current.topLevel != topLevel)
                                    {
                                        GUILayoutUtility.EndLayoutGroup();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            EditorGUIUtility.ResetGUIState();
            if (position.height > 0f)
            {
                GUI.BeginGroup(position);
                GUI.Label(new Rect(0f, 0f, position.width, position.height), "Imported Object", "OL Title");
                GUI.EndGroup();
            }
            if (this.m_Tracker.hasComponentsWhichCannotBeMultiEdited)
            {
                if (((editors.Length == 0) && !this.m_Tracker.isLocked) && (Selection.objects.Length > 0))
                {
                    this.DrawSelectionPickerList();
                }
                else
                {
                    Rect rect4 = GUILayoutUtility.GetRect(10f, 4f, EditorStyles.inspectorTitlebar);
                    if (Event.current.type == EventType.Repaint)
                    {
                        this.DrawSplitLine(rect4.y);
                    }
                    GUILayout.Label("Components that are only on some of the selected objects cannot be multi-edited.", EditorStyles.helpBox, new GUILayoutOption[0]);
                    GUILayout.Space(4f);
                }
            }
        }

        private void DrawPreviewAndLabels()
        {
            if ((this.m_PreviewWindow != null) && (Event.current.type == EventType.Repaint))
            {
                this.m_PreviewWindow.Repaint();
            }
            Editor editorThatControlsPreview = this.GetEditorThatControlsPreview(this.m_Tracker.activeEditors);
            Editor firstNonImportInspectorEditor = this.GetFirstNonImportInspectorEditor(this.m_Tracker.activeEditors);
            bool flag = (((editorThatControlsPreview != null) && this.CanPreview(editorThatControlsPreview.target)) && editorThatControlsPreview.HasPreviewGUI()) && (this.m_PreviewWindow == null);
            bool flag2 = false;
            if ((firstNonImportInspectorEditor != null) && (firstNonImportInspectorEditor.targets.Length == 1))
            {
                string assetPath = AssetDatabase.GetAssetPath(firstNonImportInspectorEditor.target);
                flag2 = assetPath.ToLower().StartsWith("assets") && !Directory.Exists(assetPath);
            }
            if (flag || flag2)
            {
                float num;
                Event current = Event.current;
                GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Height(17f) };
                Rect position = EditorGUILayout.BeginHorizontal(GUIContent.none, this.styles.preToolbar, options);
                GUILayout.FlexibleSpace();
                Rect lastRect = GUILayoutUtility.GetLastRect();
                if (flag && this.m_PreviewResizer.GetExpandedBeforeDragging())
                {
                    editorThatControlsPreview.OnPreviewSettings();
                }
                EditorGUILayout.EndHorizontal();
                if (current.type == EventType.Repaint)
                {
                    GUIContent preTitle;
                    if (flag)
                    {
                        GUIContent previewTitle = editorThatControlsPreview.GetPreviewTitle();
                        if (previewTitle != null)
                        {
                            preTitle = previewTitle;
                        }
                        else
                        {
                            preTitle = this.styles.preTitle;
                        }
                    }
                    else
                    {
                        preTitle = this.styles.labelTitle;
                    }
                    GUI.Label(position, preTitle, this.styles.preToolbar2);
                }
                if (((current.type == EventType.MouseUp) && (current.button == 1)) && (position.Contains(current.mousePosition) && (this.m_PreviewWindow == null)))
                {
                    this.DetachPreview();
                }
                if (flag)
                {
                    Rect windowPosition = base.position;
                    if (((EditorSettings.externalVersionControl != ExternalVersionControl.Disabled) && (EditorSettings.externalVersionControl != ExternalVersionControl.AutoDetect)) && (EditorSettings.externalVersionControl != ExternalVersionControl.Generic))
                    {
                        windowPosition.height -= 17f;
                    }
                    num = this.m_PreviewResizer.ResizeHandle(windowPosition, 100f, 100f, 17f, lastRect);
                }
                else
                {
                    if (GUI.Button(position, GUIContent.none, GUIStyle.none))
                    {
                        this.m_PreviewResizer.ToggleExpanded();
                    }
                    num = 0f;
                }
                if (this.m_PreviewResizer.GetExpanded())
                {
                    GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.Height(num) };
                    GUILayout.BeginVertical(this.styles.preBackground, optionArray2);
                    if (flag)
                    {
                        editorThatControlsPreview.DrawPreview(GUILayoutUtility.GetRect(0f, 10240f, (float) 64f, (float) 10240f));
                    }
                    if (flag2)
                    {
                        LabelInspector.OnLabelGUI(firstNonImportInspectorEditor.target);
                    }
                    GUILayout.EndVertical();
                }
            }
        }

        private void DrawSelectionPickerList()
        {
            if (this.m_TypeSelectionList == null)
            {
                this.m_TypeSelectionList = new TypeSelectionList(Selection.objects);
            }
            DockArea parent = base.m_Parent as DockArea;
            if (parent != null)
            {
                parent.tabStyle = "dragtabbright";
            }
            GUILayout.Space(0f);
            Editor.DrawHeaderGUI(null, Selection.objects.Length + " Objects");
            GUILayout.Label("Narrow the Selection:", EditorStyles.label, new GUILayoutOption[0]);
            GUILayout.Space(4f);
            Vector2 iconSize = EditorGUIUtility.GetIconSize();
            EditorGUIUtility.SetIconSize(new Vector2(16f, 16f));
            foreach (TypeSelection selection in this.m_TypeSelectionList.typeSelections)
            {
                GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(true) };
                Rect position = GUILayoutUtility.GetRect((float) 16f, (float) 16f, options);
                if (GUI.Button(position, selection.label, this.m_Styles.typeSelection))
                {
                    Selection.objects = selection.objects;
                    Event.current.Use();
                }
                if (GUIUtility.hotControl == 0)
                {
                    EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);
                }
                GUILayout.Space(4f);
            }
            EditorGUIUtility.SetIconSize(iconSize);
        }

        private void DrawSplitLine(float y)
        {
            Rect position = new Rect(0f, y, this.m_Pos.width + 1f, 1f);
            Rect texCoords = new Rect(0f, 1f, 1f, 1f - (1f / ((float) EditorStyles.inspectorTitlebar.normal.background.height)));
            GUI.DrawTextureWithTexCoords(position, EditorStyles.inspectorTitlebar.normal.background, texCoords);
        }

        private void DrawVCSShortInfo()
        {
            if (EditorSettings.externalVersionControl == ExternalVersionControl.AssetServer)
            {
                GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Height(17f) };
                Rect r = EditorGUILayout.BeginHorizontal(GUIContent.none, this.styles.preToolbar, options);
                UnityEngine.Object target = this.GetFirstNonImportInspectorEditor(this.m_Tracker.activeEditors).target;
                int controlID = GUIUtility.GetControlID(FocusType.Passive);
                GUILayout.FlexibleSpace();
                EditorGUILayout.EndHorizontal();
                AssetInspector.Get().OnAssetStatusGUI(r, controlID, target, this.styles.preToolbar2);
            }
            if ((EditorSettings.externalVersionControl == "Perforce") && VCProvider.isPresent)
            {
                Editor firstNonImportInspectorEditor = this.GetFirstNonImportInspectorEditor(this.m_Tracker.activeEditors);
                string assetPath = AssetDatabase.GetAssetPath(firstNonImportInspectorEditor.target);
                VCAsset assetByPath = VCDatabase.GetAssetByPath(assetPath);
                if (assetByPath != null)
                {
                    string text = this.P4Label(assetByPath);
                    if (text != string.Empty)
                    {
                        GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.Height(17f) };
                        Rect rect2 = EditorGUILayout.BeginHorizontal(GUIContent.none, this.styles.preToolbar, optionArray2);
                        GUILayout.FlexibleSpace();
                        Texture2D cachedIcon = AssetDatabase.GetCachedIcon(assetPath) as Texture2D;
                        Rect position = new Rect(rect2.x + 3f, rect2.y, 16f, 16f);
                        GUI.DrawTexture(position, cachedIcon);
                        VCOverlay.DrawOverlay(assetByPath, position);
                        Rect rect4 = new Rect(rect2.x + 21f, rect2.y, rect2.width - 21f, rect2.height);
                        GUIContent label = new GUIContent(text);
                        EditorGUI.LabelField(rect4, label, this.styles.preToolbar2);
                        string message = string.Empty;
                        if (!AssetDatabase.IsOpenForEdit(firstNonImportInspectorEditor.target, out message))
                        {
                            GUILayoutOption[] optionArray3 = new GUILayoutOption[] { GUILayout.Height(15f) };
                            if (GUILayout.Button("Checkout", this.styles.lockedHeaderButton, optionArray3))
                            {
                                EditorPrefs.SetBool("vcssticky", true);
                                VCProvider.Checkout(firstNonImportInspectorEditor.targets, CheckoutMode.Both);
                                VCDatabase.Invalidate(firstNonImportInspectorEditor.targets);
                                base.Repaint();
                            }
                        }
                        this.DrawVCSSticky(rect2.height / 2f);
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
        }

        protected virtual void DrawVCSSticky(float offset)
        {
            string message = string.Empty;
            Editor firstNonImportInspectorEditor = this.GetFirstNonImportInspectorEditor(this.m_Tracker.activeEditors);
            if (!EditorPrefs.GetBool("vcssticky") && !AssetDatabase.IsOpenForEdit(firstNonImportInspectorEditor.target, out message))
            {
                Rect rect;
                rect = new Rect(10f, base.position.height - 94f, base.position.width - 20f, 80f) {
                    y = rect.y - offset
                };
                if (Event.current.type == EventType.Repaint)
                {
                    this.styles.stickyNote.Draw(rect, false, false, false, false);
                    Rect position = new Rect(rect.x, (rect.y + (rect.height / 2f)) - 32f, 64f, 64f);
                    if (EditorSettings.externalVersionControl == "Perforce")
                    {
                        this.styles.stickyNotePerforce.Draw(position, false, false, false, false);
                    }
                    Rect rect3 = new Rect(rect.x + position.width, rect.y, rect.width - position.width, rect.height);
                    GUI.Label(rect3, new GUIContent("<b>Under Version Control</b>\nCheck out this asset in order to make changes."), this.styles.stickyNoteLabel);
                    Rect rect4 = new Rect(rect.x + (rect.width / 2f), rect.y + 80f, 19f, 14f);
                    this.styles.stickyNoteArrow.Draw(rect4, false, false, false, false);
                }
            }
        }

        private void FlipLocked()
        {
            this.isLocked = !this.isLocked;
        }

        public static InspectorWindow[] GetAllInspectorWindows()
        {
            return m_AllInspectors.ToArray();
        }

        public Editor GetEditorThatControlsPreview(Editor[] editors)
        {
            if (editors.Length != 0)
            {
                bool flag = this is PreviewWindow;
                Editor lastInteractedEditor = this.GetLastInteractedEditor();
                System.Type type = (lastInteractedEditor == null) ? null : lastInteractedEditor.GetType();
                Editor editor2 = null;
                Editor editor3 = null;
                foreach (Editor editor4 in editors)
                {
                    if ((editor4.target != null) && (!EditorUtility.IsPersistent(editor4.target) || (AssetDatabase.GetAssetPath(editor4.target) == AssetDatabase.GetAssetPath(editors[0].target))))
                    {
                        bool flag2 = !flag ? this.CanPreview(editor4.target) : true;
                        if (editor4.HasPreviewGUI() && flag2)
                        {
                            if (editor4 == lastInteractedEditor)
                            {
                                return editor4;
                            }
                            if ((editor3 == null) && (editor4.GetType() == type))
                            {
                                editor3 = editor4;
                            }
                            if (editor2 == null)
                            {
                                editor2 = editor4;
                            }
                        }
                    }
                }
                if (editor3 != null)
                {
                    return editor3;
                }
                if (editor2 != null)
                {
                    return editor2;
                }
            }
            return null;
        }

        private Editor GetFirstNonImportInspectorEditor(Editor[] editors)
        {
            foreach (Editor editor in editors)
            {
                if (!(editor.target is AssetImporter))
                {
                    return editor;
                }
            }
            return null;
        }

        public UnityEngine.Object GetInspectedObject()
        {
            if (this.m_Tracker == null)
            {
                return null;
            }
            Editor firstNonImportInspectorEditor = this.GetFirstNonImportInspectorEditor(this.m_Tracker.activeEditors);
            if (firstNonImportInspectorEditor == null)
            {
                return null;
            }
            return firstNonImportInspectorEditor.target;
        }

        internal static List<InspectorWindow> GetInspectors()
        {
            return m_AllInspectors;
        }

        public virtual Editor GetLastInteractedEditor()
        {
            return this.m_LastInteractedEditor;
        }

        public ActiveEditorTracker GetTracker()
        {
            this.CreateTracker();
            return this.m_Tracker;
        }

        private void HandleLastInteractedEditor(Rect componentRect, Editor editor)
        {
            if (((editor != this.m_LastInteractedEditor) && (Event.current.type == EventType.MouseDown)) && componentRect.Contains(Event.current.mousePosition))
            {
                this.m_LastInteractedEditor = editor;
                base.Repaint();
            }
        }

        private void MoveFocusOnKeyPress()
        {
            KeyCode keyCode = Event.current.keyCode;
            if ((Event.current.type == EventType.KeyDown) && (((keyCode == KeyCode.DownArrow) || (keyCode == KeyCode.UpArrow)) || (keyCode == KeyCode.Tab)))
            {
                if (keyCode != KeyCode.Tab)
                {
                    EditorGUIUtility.MoveFocusAndScroll(keyCode == KeyCode.DownArrow);
                }
                else
                {
                    EditorGUIUtility.ScrollForTabbing(!Event.current.shift);
                }
                Event.current.Use();
            }
        }

        private void OnDestroy()
        {
            if (this.m_PreviewWindow != null)
            {
                this.m_PreviewWindow.Close();
            }
            if ((this.m_Tracker != null) && !this.m_Tracker.Equals(ActiveEditorTracker.sharedTracker))
            {
                this.m_Tracker.Destroy();
            }
        }

        protected virtual void OnDisable()
        {
            m_AllInspectors.Remove(this);
            LabelInspector.OnDisable();
        }

        protected virtual void OnEnable()
        {
            base.title = (this.m_InspectorMode != InspectorMode.Normal) ? "UnityEditor.DebugInspectorWindow" : "UnityEditor.InspectorWindow";
            base.minSize = new Vector2(260f, 50f);
            if (!m_AllInspectors.Contains(this))
            {
                m_AllInspectors.Add(this);
            }
            this.m_PreviewResizer.Init("InspectorPreview");
        }

        protected virtual void OnGUI()
        {
            this.CreateTracker();
            this.ResetKeyboardControl();
            this.m_ScrollPosition = EditorGUILayout.BeginVerticalScrollView(this.m_ScrollPosition, new GUILayoutOption[0]);
            bool isRepaintEvent = Event.current.type == EventType.Repaint;
            if (isRepaintEvent)
            {
                this.m_Tracker.ClearDirty();
            }
            bool eyeDropperDirty = (Event.current.type == EventType.ExecuteCommand) && (Event.current.commandName == "EyeDropperUpdate");
            s_CurrentInspectorWindow = this;
            this.DrawEditors(isRepaintEvent, this.m_Tracker.activeEditors, eyeDropperDirty);
            s_CurrentInspectorWindow = null;
            EditorGUI.indentLevel = 0;
            this.AddComponentButton(this.m_Tracker.activeEditors);
            GUI.enabled = true;
            this.CheckDragAndDrop(this.m_Tracker.activeEditors);
            this.MoveFocusOnKeyPress();
            GUILayout.EndScrollView();
            if (this.m_Tracker.activeEditors.Length > 0)
            {
                this.DrawPreviewAndLabels();
                this.DrawVCSShortInfo();
            }
        }

        private void OnInspectorUpdate()
        {
            if (this.m_Tracker != null)
            {
                this.m_Tracker.VerifyModifiedMonoBehaviours();
                if (!this.m_Tracker.isDirty)
                {
                    return;
                }
            }
            base.Repaint();
        }

        private void OnLostFocus()
        {
            EditorGUI.EndEditingActiveTextField();
            LabelInspector.OnLostFocus();
        }

        private void OnSelectionChange()
        {
            this.m_TypeSelectionList = null;
            base.m_Parent.ClearKeyboardControl();
            PropertyDrawer.s_PropertyDrawers.Clear();
            base.Repaint();
        }

        private string P4Label(VCAsset asset)
        {
            if (asset.IsState(VCAsset.States.AddedLocal))
            {
                return "Added Local";
            }
            if (asset.IsState(VCAsset.States.AddedRemote))
            {
                return "Added Remote";
            }
            if (asset.IsState(VCAsset.States.CheckedOutLocal) && !asset.IsState(VCAsset.States.LockedLocal))
            {
                return "Checked Out Local";
            }
            if (asset.IsState(VCAsset.States.CheckedOutRemote) && !asset.IsState(VCAsset.States.LockedRemote))
            {
                return "Checked Out Remote";
            }
            if (asset.IsState(VCAsset.States.Conflicted) || asset.IsState(VCAsset.States.Unresolved))
            {
                return "Conflicted";
            }
            if (asset.IsState(VCAsset.States.DeletedLocal))
            {
                return "Deleted Local";
            }
            if (asset.IsState(VCAsset.States.DeletedRemote))
            {
                return "Deleted Remote";
            }
            if (asset.IsState(VCAsset.States.Local))
            {
                return "Local";
            }
            if (asset.IsState(VCAsset.States.LockedLocal))
            {
                return "Locked Local";
            }
            if (asset.IsState(VCAsset.States.LockedRemote))
            {
                return "Locked Remote";
            }
            if (asset.IsState(VCAsset.States.OutOfSync))
            {
                return "Out Of Sync";
            }
            return string.Empty;
        }

        internal static void RepaintAllInspectors()
        {
            foreach (InspectorWindow window in m_AllInspectors)
            {
                window.Repaint();
            }
        }

        private void ResetKeyboardControl()
        {
            if (this.m_ResetKeyboardControl)
            {
                GUIUtility.keyboardControl = 0;
                this.m_ResetKeyboardControl = false;
            }
        }

        private void SetDebug()
        {
            this.SetMode(InspectorMode.Debug);
        }

        private void SetDebugInternal()
        {
            this.SetMode(InspectorMode.DebugInternal);
        }

        private void SetMode(InspectorMode mode)
        {
            if (mode == InspectorMode.Normal)
            {
                base.title = "UnityEditor.InspectorWindow";
            }
            else
            {
                base.title = "UnityEditor.DebugInspectorWindow";
            }
            this.m_InspectorMode = mode;
            this.CreateTracker();
            this.m_Tracker.inspectorMode = mode;
            this.m_ResetKeyboardControl = true;
        }

        private void SetNormal()
        {
            this.SetMode(InspectorMode.Normal);
        }

        private bool ShouldCullEditor(Editor[] editors, int editorIndex)
        {
            UnityEngine.Object target = editors[editorIndex].target;
            if ((target is SubstanceImporter) || (target is ParticleSystemRenderer))
            {
                return true;
            }
            if (target.GetType() == typeof(AssetImporter))
            {
                return true;
            }
            if ((this.m_InspectorMode == InspectorMode.Normal) && (editorIndex != 0))
            {
                AssetImporterInspector inspector = editors[0] as AssetImporterInspector;
                if ((inspector != null) && !inspector.showImportedObject)
                {
                    return true;
                }
            }
            return false;
        }

        protected virtual void ShowButton(Rect r)
        {
            bool flag = GUI.Toggle(r, this.isLocked, GUIContent.none, this.styles.lockButton);
            if (flag != this.isLocked)
            {
                this.isLocked = flag;
                this.m_Tracker.RebuildIfNecessary();
            }
        }

        internal static void ShowWindow()
        {
            EditorWindow.GetWindow(typeof(InspectorWindow));
        }

        public bool isLocked
        {
            get
            {
                this.CreateTracker();
                return this.m_Tracker.isLocked;
            }
            set
            {
                this.CreateTracker();
                this.m_Tracker.isLocked = value;
            }
        }

        protected Styles styles
        {
            get
            {
                if (this.m_Styles == null)
                {
                }
                return (this.m_Styles = new Styles());
            }
        }

        [CompilerGenerated]
        private sealed class <CreateTracker>c__AnonStorey3B
        {
            internal ActiveEditorTracker sharedTracker;

            internal bool <>m__6E(InspectorWindow i)
            {
                return ((i.m_Tracker != null) && i.m_Tracker.Equals(this.sharedTracker));
            }
        }

        protected class Styles
        {
            public GUIStyle addComponentArea = EditorStyles.inspectorTitlebar;
            public GUIStyle addComponentButtonStyle = "LargeButton";
            public readonly GUIContent labelTitle = EditorGUIUtility.TextContent("InspectorLabelTitle");
            public readonly GUIStyle lockButton = "IN LockButton";
            public GUIStyle lockedHeaderButton = "preButton";
            public GUIStyle preBackground = "preBackground";
            public readonly GUIContent preTitle = EditorGUIUtility.TextContent("InspectorPreviewTitle");
            public readonly GUIStyle preToolbar = "preToolbar";
            public readonly GUIStyle preToolbar2 = "preToolbar2";
            public GUIStyle previewMiniLabel = new GUIStyle(EditorStyles.whiteMiniLabel);
            public GUIStyle stickyNote = new GUIStyle("VCS_StickyNote");
            public GUIStyle stickyNoteArrow = new GUIStyle("VCS_StickyNoteArrow");
            public GUIStyle stickyNoteLabel = new GUIStyle("VCS_StickyNoteLabel");
            public GUIStyle stickyNotePerforce = new GUIStyle("VCS_StickyNoteP4");
            public GUIStyle typeSelection = new GUIStyle("PR Label");

            public Styles()
            {
                this.typeSelection.padding.left = 12;
            }
        }
    }
}

